/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				UnitAtCondition
 
Function						: 				Provides a class for holding a 
												condition that specifies Unit 
												at a particular location	
****************************************************************************/

package planninglanguage.conditions;

import org.jdom.Element;

import util.XMLWriter;
import base.WargusGameState;
import base.WargusStateImporter;
import base.WargusUnit;


public class UnitAtCondition extends Condition {

	int m_unitID;
	int m_x;
	int m_y;
	
	public UnitAtCondition(int a_unitID,int a_x,int a_y)
	{
		m_unitID=a_unitID;
		m_x=a_x;
		m_y=a_y;
		
	}
	public String toString(int tabs)
	{
		String n = super.toString(tabs);
		n = n + "Unit Id is" + m_unitID;
		n = n + "X coordinate is" + m_x;
		n = n + "Y coordinate is" + m_y;
		
		return n;

	}
	public boolean compare(UnitAtCondition c)
	{
		if(m_unitID==c.m_unitID && m_x==c.m_x && m_y==c.m_y)
			return true;
		else
			return false;
	}

	public String getUnitType(WargusGameState a_gameState)
	{
		WargusUnit u = a_gameState.getUnit(m_unitID);

		if (u!=null) return WargusStateImporter.unitTypeToString(u.getType());
		return null;
	}
	
	
	public boolean test(WargusGameState a_gameState)
	{
		WargusUnit u = a_gameState.getUnit(m_unitID);

		if (u!=null && u.getLocX()==m_x && u.getLocY()==m_y) return true;
		return false;
	}
	
	public void writeXML(XMLWriter w) {
		w.tag("condition type=\""+this.getClass().getSimpleName()+"\"");
		{
			w.tag("unit-id", m_unitID);
			w.tag("x", m_x);
			w.tag("y", m_y);
		}
		w.tag("/condition");
	}
	
	protected static Condition loadConditionFromXML(Element e) {
		return new UnitAtCondition(Integer.parseInt(e.getChildText("unit-id")),
				Integer.parseInt(e.getChildText("x")), 
				Integer.parseInt(e.getChildText("y")));
	}
}
